let mixin = {
	data() {
		return {}
	},
	methods: {
		myPreviewImage(src) {
			if (src) {
				uni.previewImage({
					urls: [src]
				})
			} else {
				this.msg('请设置图片url！')
			}
		},
		chooseImage(option, cd, data) {
			// 将base64格式的图片转换为Blob对象 
			function dataURLtoBlob(dataURL) {
				let arr = dataURL.split(',')
				let mime = arr[0].match(/:(.*?);/)[1]
				let bstr = atob(arr[1])
				let n = bstr.length
				let u8arr = new Uint8Array(n)
				while (n--) {
					u8arr[n] = bstr.charCodeAt(n)
				}
				return new Blob([u8arr], {
					type: mime
				})
			}
			uni.chooseImage({
				...option,
				success(res) {
					let img = new Image()
					img.src = res.tempFilePaths[0]

					// 等待图片加载完成 
					img.onload = () => {
						// 创建canvas元素 
						let canvas = document.createElement('canvas')
						let ctx = canvas.getContext('2d')

						// 设置canvas尺寸 
						let width = img.width
						let height = img.height
						let scale = width / height
						let maxWidth = 800
						let maxHeight = 800 / scale
						canvas.width = maxWidth
						canvas.height = maxHeight

						// 绘制图片 
						ctx.drawImage(img, 0, 0, maxWidth, maxHeight)

						// 将canvas转换为base64格式的图片 
						let base64 = canvas.toDataURL('image/jpeg', 0.8)
						// 将base64格式的图片转换为Blob对象 
						let blob = dataURLtoBlob(base64)
						let resCopy = JSON.parse(JSON.stringify(res))
						resCopy.tempFilePaths = [URL.createObjectURL(blob)]
						cd({
							...data,
							...resCopy
						})
					}

				}
			})
		},
		chooseVideo(option, cd, data) {
			let _this = this
			uni.chooseVideo({
				...option,
				success(res) {
					if (option.maxDuration && res.duration > option.maxDuration) {
						_this.msg(`视频时长超限，最长时长为${option.maxDuration}秒`)
					} else {
						cd({
							...data,
							...res
						})
					}
				}
			})
		},
		makePhoneCall(phone) {
			if (phone) {
				uni.makePhoneCall({
					phoneNumber: phone + '',
					fail() {}
				})
			} else {
				this.msg('请设置手机号！')
			}
		},
		msg(text, path) {
			if (path && typeof path == 'number') {
				setTimeout(() => {
					uni.navigateBack({
						delta: path
					})
				}, 1200)
			} else if (path && typeof path == 'string') {
				setTimeout(() => {
					this.goto(path)
				}, 1200)
			}
			// 提示
			uni.showToast({
				title: text || '提示',
				icon: 'none',
				mask: true
			})
		},
		openLocation(obj) {
			if (obj.latitude && obj.longitude) {
				uni.openLocation({
					...obj,
					fail(e) {
						console.log(e);
					}
				});
			} else {
				this.msg('请设置经纬度信息！')
			}
		},
		goto(url) {
			uni.navigateTo({
				url,
				fail(e) {
					e.errMsg = (e.errMsg || '').toLowerCase()
					if (e.errMsg.indexOf('tabbar') !== -1 || e.errMsg.indexOf('tab bar') !== -1) {
						uni.switchTab({
							url
						})
					} else {
						uni.showToast({
							title: `${url}配置错误!`,
							icon: 'none'
						})
					}
				}
			})
		},
		formatPhone(phone) {
			// 判断手机号长度是否为11位
			if (phone && phone.length === 11) {
				return phone.substring(0, 3) + '****' + phone.substring(7);
			}
			return phone; // 如果手机号长度不符合预期，直接返回原始手机号
		}
	}
}

export default mixin